package com.example.backspring.config;

import com.google.gson.*;
import com.tencentcloudapi.common.CommonClient;
import com.tencentcloudapi.common.CommonRequest;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.SSEResponseModel;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

import java.lang.reflect.Type;

public class deepseekSample {
    public static class CustomSSEResponse extends SSEResponseModel {
        public String Response;
        private String RequestId;

        @Override
        public String getRequestId() {
            return RequestId;
        }

        @Override
        public void setRequestId(String requestId) {
            RequestId = requestId;
        }
    }

    public static class CustomSSEResponseDeserializer implements JsonDeserializer<CustomSSEResponse> {
        @Override
        public CustomSSEResponse deserialize(JsonElement jsonElement, Type type,
                                             JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
            CustomSSEResponse resp = new CustomSSEResponse();
            resp.Response = jsonElement.toString();
            return resp;
        }
    }

    public static void main(String[] args) {
        try {
            String asker = "describe deepseeR1 LLM";
            String moxing = "deepseek-r1";
            Credential cred = new Credential("AKIDMo490f7LLHBuSozxhEbmkkW4OUM0c3GF", "pRWUHrENJ3at6EAsPinfKyQeeS2OXVM0");
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("lkeap.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            CommonClient client = new CommonClient("lkeap", "2024-05-22", cred, "ap-guangzhou", clientProfile);
//            String params = "{\"Model\":\"deepseek-v3\",\"Messages\":[{\"Role\":\"user\",\"Content\":\"你好大模型\"}],\"Stream\":true,\"Temperature\":0.6,\"MaxTokens\":null}";
            // 构造包含 asker 内容的请求参数
//            String params = String.format("{\"Model\":\"deepseek-r1\",\"Messages\":[{\"Role\":\"user\",\"Content\":\"%s\"}],\"Stream\":true,\"Temperature\":0.6,\"MaxTokens\":null}", asker);
            String params = String.format("{\"Model\":\"%s\",\"Messages\":[{\"Role\":\"user\",\"Content\":\"%s\"}],\"Stream\":true,\"Temperature\":0.6,\"MaxTokens\":null}", moxing, asker);

            CommonRequest req = new CommonRequest(params);

            // 显式创建Gson对象并注册反序列化器
            Gson gson = new GsonBuilder().registerTypeAdapter(CustomSSEResponse.class,
                    new CustomSSEResponseDeserializer()).create();
            client.gson = gson;

            CustomSSEResponse resp = client.commonRequest(req, "ChatCompletions", CustomSSEResponse.class);
            if (resp.isStream()) {
                for (SSEResponseModel.SSE e : resp) {
//                    System.out.println(e.Data);
                    shuchu(e.Data);
                }
            } else {
                System.out.println(resp.Response);
            }
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }
    public static void shuchu(String jsonStr) {
        Gson gson = new Gson();
        JsonElement jsonElement = gson.fromJson(jsonStr, JsonElement.class);

        if (jsonElement.isJsonObject()) {
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            JsonArray choicesArray = jsonObject.getAsJsonArray("Choices");
            if (choicesArray != null && !choicesArray.isEmpty()) {
                for (JsonElement choiceElement : choicesArray) {
                    if (choiceElement.isJsonObject()) {
                        JsonObject choice = choiceElement.getAsJsonObject();
                        JsonObject delta = choice.getAsJsonObject("Delta");
                        if (delta != null && delta.has("Content")) {
                            String content = delta.get("Content").getAsString();
                            System.out.print(content);
                        }
                    }
                }
            }
        } else if (jsonElement.isJsonArray()) {
            JsonArray jsonArray = jsonElement.getAsJsonArray();
            for (JsonElement element : jsonArray) {
                if (element.isJsonObject()) {
                    JsonObject jsonObject = element.getAsJsonObject();
                    JsonArray choicesArray = jsonObject.getAsJsonArray("Choices");
                    if (choicesArray != null && !choicesArray.isEmpty()) {
                        for (JsonElement choiceElement : choicesArray) {
                            if (choiceElement.isJsonObject()) {
                                JsonObject choice = choiceElement.getAsJsonObject();
                                JsonObject delta = choice.getAsJsonObject("Delta");
                                if (delta != null && delta.has("Content")) {
                                    String content = delta.get("Content").getAsString();
                                    System.out.print(content);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}